// **************************************************************
// Com ++ Framework by the C++ World
// Please report defects: mailto:compp@cplusplusworld.com
// Please visit: http://www.cplusplusworld.com/socketlibrary.html
// **************************************************************

#include <iostream>
#include <cstdlib>
#include "ComPlusPlus/ComPlusPlus"

int main ( int argc, char *argv [] ) {

  if ( argc != 2 ) {
    std::cout << "Usage: " << argv [0] << "port \n";	
  	exit(1);
  }
  	
  int CPort = std::atoi ( argv[1] );	
  int port=0;
  std::string host="";
  
try {	
  int SPort = CPort + 1;
  compp::SocketUdp Srv( SPort );	
  char buf[1000];
  std::memset( (void*)buf, 0, sizeof ( buf ) );
  compp::SocketUdp Clnt(CPort);

  // ************************** 1 *********************
  Clnt.Sendto("Hello Udp1!", 11, "127.0.0.1", SPort);  
  Srv.Recv(buf, sizeof(buf),0);
  std::cout << "---" << buf << "---\n";

  // ************************** 1 *********************
  Clnt.Sendto("Hello Udp1.1!", 13, "127.0.0.1", SPort);  
  Srv.Read(buf, sizeof(buf), host, port);
  std::cout << "---" << buf << "sender host " << host << " sender port " << port << "---\n";


  // ************************** 2 *********************
  std::memset( (void*)buf, 0, sizeof ( buf ) );
  Clnt.Sendto("Hello Udp2!", 11, "127.0.0.1", SPort);  
  Srv.Connect("127.0.0.1", CPort);
  Srv.Read(buf, sizeof(buf), host, port);
  std::cout << "---" << buf << "sender host " << host << " sender port " << port <<  "---\n";
    
  // ************************** 3 *********************
  std::memset( (void*)buf, 0, sizeof ( buf ) );
  Srv.Send("Hello Udp3!", 11,0);  
  Clnt.Recv((void*)buf, 11,0);
  //Srv.Read(buf, 11, host, port);
  std::cout << "---" << buf << "---\n";

  // ************************** 4 *********************
  std::memset( (void*)buf, 0, sizeof ( buf ) );
  Clnt.Connect("127.0.0.1", SPort);
  Srv.Write("Hello Udp4!");  
  Clnt.Read(buf, 11, host, port);
  std::cout << "---" << buf << "sender host " << host << " sender port " << port << "---\n";
  
  return 0;
} catch ( syspp::ComException e ) {
  std::cout << "Exception " << e.what( ) << "\n" ;	
}  	
}
